<?php
class Menu{
	private $db;							//数据库连接
	private $main_table='sys_menu';			//主表
	private $max_level=3;					//最大级别
	
	public function __construct($db){
		$this->db=$db;
	}
   
	public function __destruct(){
		unset($this->db);
	}
	public function get_table_name(){
	    return $this->main_table;
	    
	}
	/**
	* 	insert menu
	* 	@param array $data 
	* 	@return int affected count
	*/
	public function insert_data($data){
		if(!is_array($data))
		my_jump('输入的参数有误');
		if($data['menu_level']> $this->max_level)
		my_jump('系统已限制最大'.$this->max_level.'层结构');
		$rows = $this->db->insert($this->main_table, $data);
		$insert_id = $this->db->getInsertId();
		return $insert_id;	
	}
	
	/**
	* 	update menu
	* 	@param array $data 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function update_data($data,$where){
		if(!is_array($data))
		my_jump('输入的参数有误');
		if($data['menu_level']> $this->max_level)
		my_jump('系统已限制最大'.$this->max_level.'层结构');
		$result = $this->db->update($this->main_table,$data,$where);
		return $result;
	}
	
	/**
	* 	delete menu
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function del_data($where){
		$result = $this->db->delete($this->main_table,$where);
		return $result;
	}
	


	/**
	* 	set menu (hot display etc.)
	*	@param string $default 
	*	@param string $type 
	* 	@param string $where 
	* 	@return int affected count
	*/
	public function set_data($default='',$type,$where){
		if(!$type){
			my_jump('请指定更新类型');
		}
		$set = array();
		switch($type){
			case 'common':
				if($default=='1')
				$set = array('menu_common'=>'0');
				else
				$set = array('menu_common'=>'1');
			break;
			default:
				my_jump('输入的参数有误');
			break;
		}
		$result = $this->db->update($this->main_table,$set,$where);
		return $result;
	}
	
	
	/**
	* 	get menu info
	* 	@param int $id 
	*	@param array/string $field
	* 	@return array one row data
	*/
	public function get_data_row($id,$field='*'){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE menu_id='".$id."' ";
		$data = $this->db->getRow($sql);
		return $data;
	}
	
	
	/**
	* 	get menu array
	* 	@param string $where
	* 	@return array data
	*/
	public function get_data_array($where,$order='menu_id',$limit='0',$field='*'){
		$sql = "SELECT ".$field." FROM ".$this->main_table." WHERE ".$where." ORDER BY ".$order;
		if(intval($limit)){
			$sql .= " LIMIT ".$limit;
		}
		$data = $this->db->getAll($sql);
		return $data;
	}
	
	/**
	* 	get loop menu data 用递归生成多维数组
	* 	@param int $parent_id
	*	@param array $data
	* 	@return array $data 
	*/
	public function loop_data($parent_id=0,$data){
		$array=array();
		$new_array=array();
		for($i=0;$i<count($data);$i++){
			if($data[$i]['menu_parent_id'] == $parent_id){
				$new_array = $data[$i];
				$new_array['child'] = $this->loop_data($data[$i]['menu_id'],$data);
				if(empty($new_array['child']))
					unset($new_array['child']);
				//$key = 'id'.$data[$i]['menu_id'];
				$key = $data[$i]['menu_id'];
				$array[$key] = $new_array;
			}
		}
		return $array;
	} 
	
	/**
	* 	get loop menu data 生成菜单要用的json格式
	*	@param array $data
	* 	@return string $data 
	*/
	function json_data($data){
		$nl = "\n";
		$new_json ="var guides={".$nl;
		$new_json2 ="var titles={".$nl;
		
		$num =0;
		$list2 = '';
		$common = '';
		$tab = '	';
		$tab2= '		';
		$tab3= '			';
		foreach($data as $k=>$v){
			$num ++;
			$mark = $num>1 ? "," : '';
			$new_json .= $tab.$mark."'".$k."' : {".$nl;
			$data2 = $v['child'];
			$num3 = 0;
			if($data2){
				foreach($data2 as $k1=>$v1){
					$num2 ++ ;
					$num3 ++ ;
					$mark2 = $num2>1 ? "," : '';
					$mark3 = $num3>1 ? "," : '';
					$new_json .= $tab2.$mark3."'".$k1."' : {".$nl;
					$row2 = "'".$k1."' : '".$v1['menu_title']."'";
					$list2 .= $mark2.$row2.$nl;
					$list = '';
					
					$data2 = $v1['child'];
					if($data2){
						foreach($data2 as $k2=>$v2){
						
							$row = "'".$k2."' : ['".$v2['menu_title']."','".$v2['menu_link']."','".$v2['menu_target']."']".$nl;
							$list .= $list ? $tab3.",".$row : $tab3.$row;
							//是否为日常工具
							if($v2['menu_common']){
								$common .= $common ? $tab3.",".$row : $tab3.$row;
							}
						}
					}
					$new_json .= $list;
					$new_json .= $tab2."}".$nl;
				}
			}
			
			$new_json .= $tab."}".$nl;
		}
		$new_json2 .= $list2;
		
		
		//添加日常工具
		if($common){
			$new_json .= $tab.",'common' : {".$nl;
			$new_json .= $tab2."'common_' : {".$nl;
			$new_json .= $common;
			$new_json .= $tab."}}".$nl;
			$new_json2 .= ",'common_' : '日常工具'".$nl;
		}
		
		$new_json .="}".$nl;
		$new_json2 .="}".$nl;
		$data = $new_json."\n".$new_json2;
		return $data;
	} 

	public function load_data($file='var_menu.php'){
		$file = INFO_PATH.$file;
		if(is_file($file)){
			require($file);
			return $menu_array;
		}else{
			die("文件加载失败");
		}
	}
	
	public function get_dpt_row($menudata,$dpt='') {
		$num=0;
		if(intval($dpt) && $menudata[$dpt]){
			$row= $menudata[$dpt];
		}else{
			if($menudata){
				foreach($menudata as $item){
					if($num=='0'){
						$row = $item;
						break;
					}
					$num++;
				}
			}
		}
		return $row;
	}

	public function get_dpt_menu($data,$dpt){
		global $cf_controller;
		
		if($data){
		
			foreach ($data as $k=>$v){
				$basename=substr($v['menu_link'],6,(strpos($v['menu_link'],".htm")-6));
				if($basename == $cf_controller){
					$data[$k]['class']='active';
				}else{
					$data[$k]['class']='';
				}
			}
		}
		return $data;
	}
	
		/**
	* 	update menu file
	* 	@param array $data 
	*/
	public function update_data_file($type=0){
		if(file_exists(INFO_PATH.'var_menu1.php')){
   			@unlink(INFO_PATH.'var_menu1.php');
  		}
		$type = intval($type);
		if($type>1){
			load_file('power_group_class',LIB_PATH);
			$power_group = new PowerGroup($this->db);
			$row = $power_group->get_group_row($type,'g_view_power');
			$view_power = $row['g_view_power'];
			if($view_power){
				$menu_array = $this->get_data_array("menu_id in (".$view_power.")",'menu_level ASC,menu_order ASC');
				$menu = $this->loop_data(0,$menu_array);
				$new_array = var_export($menu,true);
				$input_data= "\$menu_array=".$new_array;
				write_file(INFO_PATH."var_menu".$type.".php",$input_data);
				//$input_data = $this->json_data($menu);
				//write_file(INFO_PATH."menu".$type.".js",$input_data,'js');
			}

		}else{
			//$this->db->setDebug(1,1);
			$type = ($type=='1') ? $type : '';
			$menu_array = $this->get_data_array("1",'menu_level ASC,menu_order ASC');
			$menu = $this->loop_data(0,$menu_array);
			$new_array = var_export($menu,true);
			$input_data= "\$menu_array=".$new_array;
			write_file(INFO_PATH."var_menu".$type.".php",$input_data);
			
			//$input_data = $this->json_data($menu);
			//write_file(INFO_PATH."menu.js",$input_data,'js');

		}

	}
}

?>